//
//  OADTitleHeaderField.m
//  OASDK_Works
//
//  Created by ChenJie on 5/21/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADTitleHeaderField.h"
#import "OASDK.h"

#define kTitleFont [UIFont systemFontOfSize:16]
#define kMarginH 10
#define kMarginV 5

typedef NS_ENUM(NSUInteger, ComponentId) {
    TitleLabel,
};

@interface OADTitleHeaderField ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, assign) CGSize titleLabelSize;

@end

@implementation OADTitleHeaderField

#pragma mark - Lifecycle methods

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.titleLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    CGRect titleFrame = [self.viewModel.layouts[@(TitleLabel)] CGRectValue];
    titleFrame.origin.x = (self.oa_width - titleFrame.size.width) / 2;
    titleFrame.origin.y = (self.oa_height - titleFrame.size.height) / 2;
    self.titleLabel.frame = titleFrame;
    
    //
    [self oa_showBounds];
}

#pragma mark - Public methods

/* dynamic viewHeight */
+ (CGFloat)heightWithViewWidth:(CGFloat)viewWidth
                     viewModel:(OADTitleHeaderFieldViewModel *)viewModel {
    
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    
    CGFloat topMark = 0;
    
    //
    topMark += kMarginV;
    CGFloat titleWidth = viewWidth - kMarginH * 2;
    CGSize titleLabelSize = [viewModel.title oa_sizeWithWidth:titleWidth font:kTitleFont];
    CGRect titleLabelFrame = CGRectMake(kMarginH, topMark, titleLabelSize.width, titleLabelSize.height);
    layouts[@(TitleLabel)] = [NSValue valueWithCGRect:titleLabelFrame];
    topMark += titleLabelSize.height;
    
    viewModel.layouts = layouts;
    
    return topMark + kMarginV;
}

/* static viewHeight */
+ (void)layoutWithViewSize:(CGSize)viewSize viewModel:(OADTitleHeaderFieldViewModel *)viewModel {
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    
    CGFloat topMark = 0;
    
    //
    topMark += kMarginV;
    CGFloat titleWidth = viewSize.width - kMarginH * 2;
    CGSize titleLabelSize = [viewModel.title oa_sizeWithWidth:titleWidth font:kTitleFont];
    CGRect titleLabelFrame = CGRectMake(kMarginH, topMark, titleLabelSize.width, titleLabelSize.height);
    layouts[@(TitleLabel)] = [NSValue valueWithCGRect:titleLabelFrame];
    
    viewModel.layouts = layouts;
}

#pragma mark - Private methods



#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(OADTitleHeaderFieldViewModel *)viewModel {
    _viewModel = viewModel;
    
    //
    self.titleLabel.text = viewModel.title;
}

#pragma mark - Getter

OAGetLabel(titleLabel, ^(UILabel *label){
    label.font = kTitleFont;
    label.backgroundColor = OARandomColor();
    label.numberOfLines = 0;
});

#pragma mark - Memory management methods

- (void)dealloc {
    OALogFunction;
}

@end
